package com.fenghongzhang.day004;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class IndexActivity extends AppCompatActivity {

    private LinearLayout ll;
    private RadioGroup group;
    private RadioButton index;
    private RadioButton me;
    private  FragmentManager manager;
    private BlankFragment blankFragment;
    private BlankFragment2 blankFragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);


        ll = (LinearLayout) findViewById(R.id.ll);
        group = (RadioGroup) findViewById(R.id.group);
        index = (RadioButton) findViewById(R.id.r_index);
        me = (RadioButton) findViewById(R.id.r_me);


        manager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
         blankFragment = new BlankFragment();
        fragmentTransaction.add(R.id.ll,blankFragment);
         blankFragment2 = new BlankFragment2();
        fragmentTransaction.add(R.id.ll,blankFragment2);
        fragmentTransaction.hide(blankFragment2);
        fragmentTransaction.commit();

        //切换
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.r_index:
                        manager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = manager.beginTransaction();
                        fragmentTransaction.hide(blankFragment2);
                        fragmentTransaction.show(blankFragment);
                        fragmentTransaction.commit();
                        break;
                    case R.id.r_me:
                        manager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction1 = manager.beginTransaction();
                        fragmentTransaction1.hide(blankFragment);
                        fragmentTransaction1.show(blankFragment2);
                        fragmentTransaction1.commit();
                        break;
                }

            }
        });

    }
}
